package com.aaron.gesturehelper;

import android.content.Context;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;
import android.widget.TextView;

public class SeekBarPreference extends Preference implements SeekBar.OnSeekBarChangeListener {

	public SeekBarPreference(Context context, AttributeSet attrs) {
		super(context, attrs);
		setLayoutResource(R.layout.myseekbar);
	}

	public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
		persistInt(progress);
		MyConfiguration.notifyConfigChanged(getContext(), getKey());
	}

	public final void onStartTrackingTouch(SeekBar seekBar) {
	}

	public final void onStopTrackingTouch(SeekBar seekBar) {
	}

	protected final void onBindView(View view) {
		super.onBindView(view);
		
		SeekBar barView = (SeekBar) ((ViewGroup) view).findViewById(R.id.seekbar);
		TextView title = (TextView) view.findViewById(R.id.title);
		TextView left = (TextView) view.findViewById(R.id.left);
		TextView right = (TextView) view.findViewById(R.id.right);
		
		String key = getKey();
		barView.setMax(4);
		if(key.equals(MyConfiguration.GESTUREAREA_EXPAND_TIME)) {
			barView.setProgress(getPersistedInt(1));
			title.setText(R.string.setexpandtime);
			left.setText(R.string.expand_shorttime);
			right.setText(R.string.expand_longtime);
		} else if(key.equals(MyConfiguration.GESTUREAREA_HIDE_TIME)) {
			barView.setProgress(getPersistedInt(0));
			title.setText(R.string.sethidetime);
			left.setText(R.string.hide_shorttime);
			right.setText(R.string.hide_longtime);
		} else if(key.equals(MyConfiguration.GESTUREAREA_WIDTH)) {
			barView.setProgress(getPersistedInt(4));
			title.setText(R.string.setgesturewidth);
			left.setText(R.string.narrow);
			right.setText(R.string.wide);
		} else if(key.equals(MyConfiguration.GESTUREAREA_HEIGHT)) {
			barView.setProgress(getPersistedInt(2));
			title.setText(R.string.setgestureheight);
			left.setText(R.string.low);
			right.setText(R.string.high);
		}
		barView.setOnSeekBarChangeListener(this);
	}
}